<template>
	<view class="page" v-if="data.detail.files.length">
		<!-- 主图区域 -->
		<view class="preview-box">
			<view class="main-image">
				<view class="preview-image" :style="{ 'backgroundImage': 'url(' + previewImage + '?style=imageMogr2/thumbnail/1200x)' }" v-if="previewImage"></view>
				<view class="preview-image" v-else>
					<video :src="data.detail.movieUrl" class="video" object-fit="fill" :poster="data.detail.movieThumbnailUrl+'?style=imageMogr2/thumbnail/1200x'"></video>
				</view>
			</view>
		</view>
		<view class="ai-text">*该功能由AI生成、人工智能生成</view>
		<!-- 底部信息 -->
		<view class="bottom-info">
			<view class="template">{{ data.detail.name }}</view>
			<view class="usage-count">
				<img src="https://gamumu.hilumo.com/prod-api/files/down/1963057499998040064.png" alt="fire" class="fire">
				{{ data.detail.usedNum }}人 使用过
			</view>
		</view>
		<view class="photo-count" v-if="data.detail.description">{{ data.detail.description }}</view>
		<view class="photo-count" v-else>一套风格包含{{ data.detail.files.length }}张照片<text v-if="data.detail.movieInfo && data.detail.movieInfo.uuid">、1个视频</text>写真</view>
		<view class="attribute-box">
			属性：
			<view class="tag">{{ locationName }}</view>
		</view>
		<!-- 缩略图预览 -->
		<view class="scroll-box">
			<scroll-view class="thumbnail-scroll" scroll-x>
				<view class="thumbnail-container">
					<view v-for="(item, index) in data.detail.files" :key="index" class="thumbnail-item"
						:class="{'current': index === thumbnailIndex}" @click="clickThumbnail(index)">
						<image class="thumbnail" :src="item.url+'?style=imageMogr2/thumbnail/1200x'" mode="aspectFill"></image>
					</view>
					<view v-if="data.detail.movieInfo && data.detail.movieInfo.uuid" class="thumbnail-item" :class="{'current': data.detail.files.length === thumbnailIndex}" @click="clickThumbnail(data.detail.files.length)">
						<image class="thumbnail" :src="data.detail.movieThumbnailUrl+'?style=imageMogr2/thumbnail/1200x'" mode="aspectFill"></image>
						<view class="mask">
							<up-icon name="play-right-fill" color="#ffffff" size="20" class="play"></up-icon>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		
		<view class="attribute-box" v-if="data.detail.movieInfo && data.detail.movieInfo.uuid">
			制作类型：
			<view style="display: inline-block;">
				<up-radio-group
					v-model="data.type"
					placement="row"
				>
					<up-radio
						:customStyle="{marginRight: '16px'}"
						v-for="(item, index) in data.types"
						:key="index"
						:label="item.name"
						:name="item.value"
						activeColor="#D19973"
						labelSize="10"
						labelColor="#7F7F7F"
						size="14"
					>
					</up-radio>
				</up-radio-group>
			</view>
		</view>

		<!-- 底部提示与按钮 -->
		<view class="bottom-bar">
			<button v-if="token && token !== ''" class="make-button" type="primary" @click="clickMake(data.detail)">{{ pirceMoney }}元制作</button>
			<button v-else class="make-button" type="primary" @click="handleLogin">{{ pirceMoney }}元制作</button>
		</view>
		<!-- 登录弹窗 -->
		<LoginPopup ref="loginPopupRef" @success="handleLoginSuccess" />
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		computed,
		onMounted
	} from 'vue'
	import {
		onLoad,
		onShow,
		onShareAppMessage,
		onShareTimeline
	} from '@dcloudio/uni-app'
	import {
		timeFormat
	} from '@/uni_modules/uview-plus';
	import uma from '../../uma.js';
	
	const token = ref(null)
	const data = reactive({
		ownTypes: [],
		catTypes: [],
		detail: {
			files: [],
			price: 0
		},
		type: 0,
		types: [
			{
				name: '全图片',
				value: 0
			},
			{
				name: '一张图片+视频',
				value: 1
			},
		]
	})
	const bSwitch = ref(false)
	// 获取token
	onShow(() => {
		uni.removeStorageSync('makeData')
		token.value = uni.getStorageSync('token')
		if (token.value && token.value != '') {
			getMyCoupon()
		}
	})

	// 归属地名称
	const locationName = computed(() => {
		if (data.ownTypes.length && data.detail.uuid) {
			const location = data.ownTypes.filter(item => item.dictValue === data.detail.ownType)[0]
			return location.dictLabel
		} else {
			return ''
		}
	})
	
	// 套系分类
	const catTypeName = computed(() => {
		if (data.catTypes.length && data.detail.uuid) {
			const item = data.catTypes.filter(item => item.dictValue === data.detail.catType)[0]
			return item.dictLabel
		} else {
			return ''
		}
	})
	
	// 价格，0全照片取price,1一照片+视频moviePirce
	const pirceMoney = computed(() => {
		let money
		switch (data.type){
			case 0:
				money = data.detail.price / 100
				break;
			case 1:
				money = data.detail.moviePrice / 100
				break;
			default:
				money = data.detail.price / 100
				break;
		}
		return money
	})
	
	// 上报微信管理数据
	function upWxData(data) {
		wx.reportEvent('detail_type', data)
		// #ifdef MP-WEIXIN
		uma.trackEvent('detail_type', data)
		// #endif
	}
	
	const uuid = ref(null)
	onLoad((option) => {
		uuid.value = option.uuid
	})

	onMounted(() => {
		getDictOwnType()
		getDictCatType()
		setTimeout(() => {
			getTemplate()
		}, 500)
	})
	
	// 登录
	const loginPopupRef = ref(null)
	function handleLogin() {
		loginPopupRef.value.open()
	}
	function handleLoginSuccess() {
		token.value = uni.getStorageSync('token')
	}
	
	// 获取详情
	function getTemplate() {
		uni.request({
			url: `${getApp().globalData.baseUrl}/prod-api/system/photos/${uuid.value}`,
			method: 'GET',
			success: (res) => {
				if (res.data.code === 200) {
					data.detail = res.data.data
					// 套系分类上报微信数据管理
					// #ifdef MP-WEIXIN
					const updateData = {
						location_id: data.detail.ownType,
						location_name: locationName.value,
						template_id: data.detail.uuid,
						template_name: data.detail.name,
						cat_type_id: data.detail.catType,
						cat_type: catTypeName.value,
						time: timeFormat(new Date().getTime(), 'yyyy-mm-dd hh:MM:ss')
					}
					upWxData(updateData)
					// #endif
				}
			},
			fail: (error) => {
				console.log(error);
			}
		});
	}

	// 获取套系归属地
	function getDictOwnType() {
		uni.request({
			url: `${getApp().globalData.baseUrl}/prod-api/system/dict/data/list`,
			data: {
				dictType: 'template_own_type',
				pageNum: 1,
				pageSize: 9999
			},
			method: 'GET',
			success: (res) => {
				if (res.data.code === 200) {
					data.ownTypes = res.data.rows
				}
			},
			fail: (error) => {
				console.log(error);
			}
		});
	}
	
	// 获取套系分类
	function getDictCatType() {
		uni.request({
			url: `${getApp().globalData.baseUrl}/prod-api/system/dict/data/list`,
			data: {
				dictType: 'template_cat_type',
				pageNum: 1,
				pageSize: 9999
			},
			method: 'GET',
			success: (res) => {
				if (res.data.code === 200) {
					data.catTypes = res.data.rows
				}
			},
			fail: (error) => {
				console.log(error);
			}
		});
	}

	// 缩略图index
	const thumbnailIndex = ref(0)
	// 预览大图
	const previewImage = computed(() => {
		if (thumbnailIndex.value === data.detail.files.length) {
			return null
		} else {
			return data.detail.files.length ? data.detail.files[thumbnailIndex.value].url : null
		}
	})

	// 点击缩略图
	function clickThumbnail(index) {
		thumbnailIndex.value = index
	}

	// 制作
	function clickMake(item) {
		console.log(data.type, thumbnailIndex.value, data.detail.files.length)
		if (data.type == 1 && thumbnailIndex.value == data.detail.files.length) {
			uni.showToast({
				icon: 'none',
				title: '请选择需制作的图片'
			})
			return
		}
		const imgItem = data.detail.files[thumbnailIndex.value]
		const makeData = {
			type: data.type
		}
		if (data.type == 1) {
			makeData.iuuid = `${imgItem.uuid}`
		}
		uni.setStorageSync('makeData', makeData)
		uni.navigateTo({
			url: `/pages/upload/upload?uuid=${item.uuid}`
		})
	}
	
	// 分享
	onShareAppMessage(res => {
		return {
			title: data.detail.name,
			path: `/pages/detail/detail?uuid=${uuid.value}`
		}
	})
	
	// 分享到朋友圈
	onShareTimeline(res => {
		return {
			title: data.detail.name,
			path: `/pages/detail/detail?uuid=${uuid.value}`
		}
	})
	
	// 获取用户信息，目的是验证token 是否过期，code = 401 
	function getMyCoupon() {
		uni.request({
			url: `${getApp().globalData.baseUrl}/prod-api/getInfo`,
			method: 'GET',
			header: {
				'Authorization': "Bearer " + token.value,
			},
			success: (res) => {
				if (res.data.code === 401) {
					uni.removeStorageSync('token')
					uni.removeStorageSync('user')
					uni.removeStorageSync('makeData')
					token.value = null
				}
			}
		});
	}
</script>

<style lang="scss" scoped>
	.page {
		padding-bottom: 210rpx;
	}
	.scroll-box {
		padding: 0 40rpx;
	}

	.location {
		padding: 20rpx 40rpx;
		text-align: center;
		font-size: 28rpx;
		color: #999999;
	}

	.preview-box {
		display: flex;
		flex-direction: column;
		background-color: #ffffff;
		padding: 0 40rpx;
	}

	.main-image {
		flex: 1;
		width: 100%;
		position: relative;
		height: 860rpx;
		overflow: hidden;
	}

	.preview-image {
		width: 100%;
		height: 860rpx;
		border-radius: 12rpx;
		background-size: 670rpx auto;
		background-repeat: no-repeat;
		background-position: center;
		.video {
			width: 100%;
			height: 860rpx;
		}
	}

	.bottom-info {
		padding: 0 40rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.template {
			font-family: PingFang SC;
			font-weight: 600;
			font-style: Heavy;
			font-size: 28rpx;
			leading-trim: NONE;
			line-height: 40rpx;
			letter-spacing: 0%;
		}
	}

	.usage-count {
		font-family: PingFang SC;
		font-weight: 400;
		font-style: Medium;
		font-size: 20rpx;
		leading-trim: NONE;
		line-height: 40rpx;
		letter-spacing: 0;
		color: #7F7F7F;
		margin-bottom: 10rpx;
		.fire {
			width: 32rpx;
			height: 32rpx;
			vertical-align: middle;
			margin-top: -2px;
		}
	}
	.photo-count {
		font-weight: 400;
		font-style: Medium;
		font-size: 20rpx;
		leading-trim: NONE;
		line-height: 40rpx;
		letter-spacing: 0;
		color: #7F7F7F;
		margin-bottom: 10rpx;
		padding: 0 40rpx;
	}
	.attribute-box {
		margin-bottom: 10rpx;
		padding: 0 40rpx;
		font-weight: 400;
		font-style: Medium;
		font-size: 20rpx;
		leading-trim: NONE;
		line-height: 40rpx;
		letter-spacing: 0;
		color: #7F7F7F;
		.tag {
			display: inline-block;
			border-radius: 20rpx;
			padding: 8rpx 12rpx;
			background: #FFD19C26;
			font-family: PingFang SC;
			font-weight: 400;
			font-style: Medium;
			font-size: 20rpx;
			leading-trim: NONE;
			line-height: 28rpx;
			letter-spacing: 0%;
			color: #D19973;
		}
	}
	.thumbnail-scroll {
		width: 100%;
		white-space: nowrap;
	}

	.thumbnail-container {
		display: inline-flex;
	}

	.thumbnail-item {
		margin-right: 10rpx;
		width: 120rpx;
		height: 160rpx;
		border-radius: 12rpx;
		overflow: hidden;
		border: 6rpx solid #FFFFFF;
		transition: all 0.5s;
		box-sizing: border-box;
		position: relative;
		.mask {
			width: 108rpx;
			height: 148rpx;
			position: absolute;
			top: 0;
			left: 0;
			background-color: rgba(0, 0, 0, 0.4);
			.play {
				position: absolute;
				top: 50%;
				left: 50%;
				margin-top: -20rpx;
				margin-left: -20rpx;
			}
		}
		&.current {
			border-color: #D19973;
		}
	}

	.thumbnail-item:last-child {
		margin-right: 0;
	}

	.thumbnail {
		width: 100%;
		height: 100%;
	}

	.bottom-bar {
		padding: 20rpx 40rpx 20rpx;
		padding-bottom: env(safe-area-inset-bottom);
		background: #ffffff;
		position: fixed;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
	}

	.make-button {
		color: #ffffff;
		font-family: PingFang SC;
		font-weight: 500;
		font-style: Heavy;
		font-size: 36rpx;
		leading-trim: NONE;
		line-height: 100rpx;
		letter-spacing: 10%;
		border-radius: 28rpx;
		background: linear-gradient(90deg, #D19973 0%, #EBBD9D 100%);
		width: 422rpx;
		height: 100rpx;
		margin-bottom: 20rpx;
	}

	.make-button:active {
		opacity: 0.8;
	}
</style>